﻿//* Copyright(C) 2005-2014, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
 **************************************************************************
 */

/**
 * 文档信息插件类；
 */
define(function (require, exports, module) {
    /**
     * Plugin of document properties.
     * @param {WebPDF.ReaderApp} readerApp app instance
     * @extends {WebPDF.IReaderPlugin}
     * @constructor
     */
    WebPDF.PDFDocPropertiesPlugin = function (readerApp) {
        var _docInfoDlgHtml = "",
            _readerApp = readerApp,
            _isDlgInitialized = false,
            _docInfoDlg = null,
            _appId = _readerApp.getAppId();
            //_self = this;

        var _fieldIDs = {
            author: _appId + "_docPropertiesAuthor",
            creator: _appId + "_docPropertiesCreator",
            producer: _appId + "_docPropertiesProducer",
            title: _appId + "_docPropertiesTitle",
            subject: _appId + "_docPropertiesSubject",
            keywords: _appId + "_docPropertiesKeywords",
            creationDate: _appId + "_docPropertiesCreationDate",
            modifyDate: _appId + "_docPropertiesModifyDate",
            version: _appId + "_docPropertiesVersion",
            name: _appId + "_docPropertiesName",
            pageCount: _appId + "_docPropertiesPageCount",
            pageSize: _appId + "_docPropertiesPageSize",
            unit: _appId + "_docPropertiesUnit",
            closeBtn : _appId + "_btnCloseDocPropertiesDlg"
        };

        /**
         * Convert value from point to target unit.
         * @param targetUnit target unit
         * @param value value to convert
         * @returns {Number}
         * @private
         */
        function _convertValueFromPointUnit(targetUnit, value) {
            var ptValue = WebPDF.Common.changeUnit(value, "point", targetUnit);
            return WebPDF.Common.parseValueToDecimal(ptValue, 2);
        }

        //获取Plugin名称;
        this.getPluginName = function () {
            return "DocProperties Plugin";
        };
        //插件被注册的时候调用;
        this.onRegister = function () {
        };
        //模块初始化;
        this.init = function () {
            _createCmdAndBtn();
        };
        //模块卸载;
        this.unload = function () {
        };

        function _popupPropertiesDlg() {
            var docProperties = _readerApp.getPDFDoc().getDocProperties();
            if (!docProperties) {
                return;
            }
            var appId = _readerApp.getAppId();
            var propertiesDlgId = appId + "-fwr-doc-properties-dlg";
            if (!_isDlgInitialized) {
                _docInfoDlgHtml = "<div id='" + propertiesDlgId
                                        + "' style='display:none' class='fwr-modal fwr-hide fwr-fade fwr-in fwr-doc-properties-dlg'>" +
                        "<div class='fwr-modal-header'>" +
                            "<a class='close' webpdf-data-dismiss='modal'></a>" +
                            "<h4 data-i18n='DocProperties.Title'>Foxit Web PDF</h4>" +
                        "</div>" +
                        "<div class='fwr-modal-body'>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.PDFTitle'></span><span id='" + _fieldIDs.title +"' class='txtspan' title=''></span></div>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.Subject'></span><span id='" + _fieldIDs.subject + "' class='txtspan'></span></div>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.Author'></span><span id='" + _fieldIDs.author +"' class='txtspan'></span></div>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.Creator'></span><span id='" + _fieldIDs.creator + "' class='txtspan'></span></div>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.Producer'></span><span id='" + _fieldIDs.producer + "' class='txtspan'></span></div>" +
                            "<div class='sp'><span class='left' style='float:left' data-i18n='DocProperties.Keywords'></span><textarea id='" + _fieldIDs.keywords +"' style='width:272px;max-width:272px;height:55px; resize:none;' class='fwr-doc-properties-dlg-keyword' disabled='disabled'></textarea></div>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.CreateDate'></span><span  id='" + _fieldIDs.creationDate + "' class='txtspan'></span></div>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.ModifyDate'></span><span  id='" + _fieldIDs.modifyDate + "' class='txtspan'></span></div>" +
                            "<div class='separator'></div>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.PDFVersion'></span><span id='" + _fieldIDs.version + "'></span></div>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.PageSize'></span><span id='" + _fieldIDs.pageSize + "'></span>" +
                                "<select id='" + _fieldIDs.unit + "' style='width:53px;margin-left:4px'>" +
                                    "<option value='inch' data-i18n='DocProperties.Inch'></option>" +
                                    "<option value='cm' data-i18n='DocProperties.Centimeter'></option>" +
                                    "<option value='point' data-i18n='DocProperties.Point'></option>" +
                                    "<option value='pica' data-i18n='DocProperties.Pica'></option>" +
                                "</select>" +
                            "</div>" +
                            "<div class='sp'><span class='left' data-i18n='DocProperties.NumOfPages'></span><span id='" + _fieldIDs.pageCount + "'></span></div>" +
                        "</div>" +
                        "<div class='fwr-modal-footer' >" +
                            "<a href='#' id='" + _fieldIDs.closeBtn + "' class='btn btn-success' webpdf-data-dismiss='modal' data-i18n='DocProperties.OKBtnTitle'>Close</a>" +
                        "</div>" +
                    "</div>";

                $('#' + readerApp.getMainFrameId()).append(_docInfoDlgHtml);
                _docInfoDlg = $("#" + propertiesDlgId);
                $("#" + _fieldIDs.unit).on("change", function () {
                    _onChangeUnit();
                });
                _isDlgInitialized = true;
            }

            if (_docInfoDlg) {
                _docInfoDlg.i18n();
                var authorTitle = docProperties.getAuthor();
                if (authorTitle) {
                    $("#" + _fieldIDs.author).text(authorTitle).attr("title", authorTitle);
                }
                var creatorTitle = docProperties.getCreator();
                if (creatorTitle) {
                    $("#" + _fieldIDs.creator).text(creatorTitle).attr("title", creatorTitle);
                }
                var producerTitle = docProperties.getProducer();
                if (producerTitle) {
                    $("#" + _fieldIDs.producer).text(producerTitle).attr("title", producerTitle);
                }
                var titleTitle = docProperties.getTitle();
                if (titleTitle) {
                    $("#" + _fieldIDs.title).text(titleTitle).attr("title", titleTitle);
                }
                var subjectTitle = docProperties.getSubject();
                if (subjectTitle) {
                    $("#" + _fieldIDs.subject).text(subjectTitle).attr("title", subjectTitle);
                }
                var keywordsTitle = docProperties.getKeywords();
                if (keywordsTitle) {
                    $("#" + _fieldIDs.keywords).val(keywordsTitle);
                }
                var creationDate = docProperties.getCreationDate();
                if (creationDate) {
                    $("#" + _fieldIDs.creationData).text(creationDate).attr("title", creationDate);
                }
                var modifyDate = docProperties.getModifyDate();
                if (modifyDate) {
                    $("#" + _fieldIDs.modifyDate).text(modifyDate).attr("title", modifyDate);
                }
                var version = docProperties.getVersion();
                if (version) {
                    $("#" + _fieldIDs.version).text(version).attr("title", version);
                }
                var pageCount = _readerApp.getMainView().getDocView().getPageCount();
                $("#" + _fieldIDs.pageCount).text(pageCount).attr("title", pageCount);
                _onChangeUnit();
                _docInfoDlg.webPDFModal({keyboard:false, backdrop:"static", manager:"#"+_readerApp.getMainFrameId()});
            }
        }

        function _onChangeUnit() {
            var unit = $('#' + _fieldIDs.unit).val();
            var curPageIndex = _readerApp.getMainView().getDocView().getCurPageIndex();
            var pdfPage = _readerApp.getPDFDoc().getPage(curPageIndex);
            var w = _convertValueFromPointUnit(unit, pdfPage.getPageWidth());
            var h = _convertValueFromPointUnit(unit, pdfPage.getPageHeight());
            var pageSize = w + " * " + h;
            $("#" + _fieldIDs.pageSize).text(pageSize).attr("title", pageSize);
        }

        function _createCmdAndBtn() {
            var popupDocPropertiesMenuId = _appId + "contextMenuDocProperties";
            _readerApp.addMenuItem({
                name: "MenuDocProperties",
                createHtml: function () {
                    return "<li  id='" + popupDocPropertiesMenuId + "' class='icon' menuname='MenuDocProperties'><span class='icon icon1'></span>"
                        + "<label data-i18n='DocProperties.Title'></label></li>";
                },
                onShow: function () {
                },
                onSelect: function (jqMenuItem) {
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled"))
                            _popupPropertiesDlg();
                        else
                            return false;
                    }
                }
            });
        }
    };

    exports.createPlugin = function (readerApp) {
        return new WebPDF.PDFDocPropertiesPlugin(readerApp);
    };
});